package com.qlylkj.lbk.ui.cochain

import android.view.View
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.forest.core.etx.startKtxActivity
import com.google.android.material.tabs.TabLayoutMediator
import com.qlylkj.lbk.R
import com.qlylkj.lbk.app.PreferenceHelper
import com.qlylkj.lbk.base.BaseFragment
import com.qlylkj.lbk.ui.company.CompanyInfoFragment
import com.qlylkj.lbk.ui.company.EditCompanyInfoActivity
import com.qlylkj.lbk.ui.user.authentication.IdentityAuthActivity
import kotlinx.android.synthetic.main.fragment_cochain.*
import me.bakumon.statuslayoutmanager.library.OnStatusChildClickListener
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager

class CoChainFragment : BaseFragment() {

    private val titleList = mutableListOf("公司信息", "上链记录")

    private val companyInfoFragment by lazy { CompanyInfoFragment.newInstance() }

    private val coChainRecordFragment by lazy { CoChainRecordFragment.newInstance() }

    private val fragmentList by lazy { mutableListOf(companyInfoFragment, coChainRecordFragment) }

    private val statusLayoutManager by lazy { initStatusLayoutManager() }

    companion object {
        fun newInstance(): CoChainFragment {
            return CoChainFragment()
        }
    }

    override fun setLayoutId() = R.layout.fragment_cochain

    override fun initView() {
        initViewPager()
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        PreferenceHelper.getUserInfo({
            it.apply {
                if (bindingStatus == 1) {
                    statusLayoutManager.showSuccessLayout()
                } else {
                    statusLayoutManager.showCustomLayout(
                        R.layout.layout_uncertified_company,
                        R.id.enterCompany,
                        R.id.identityAuth
                    )
                }
            }
        })
    }

    private fun initStatusLayoutManager(): StatusLayoutManager {
        return StatusLayoutManager.Builder(coChainLayout).apply {
            setOnStatusChildClickListener(object : OnStatusChildClickListener {

                override fun onErrorChildClick(view: View?) {

                }

                override fun onEmptyChildClick(view: View?) {

                }

                override fun onCustomerChildClick(view: View?) {
                    when (view?.id) {
                        R.id.enterCompany -> toEnterCompanyInfo()
                        R.id.identityAuth -> toIdentityAuthentication()
                        else -> {
                        }
                    }
                }
            })
        }.build()
    }

    private fun initViewPager() {
        cochainViewPager.apply {
            adapter = object : FragmentStateAdapter(this@CoChainFragment) {
                override fun createFragment(position: Int) = fragmentList[position]

                override fun getItemCount() = fragmentList.size
            }
        }
        TabLayoutMediator(coChainTabLayout, cochainViewPager) { tab, position ->
            tab.text = titleList[position]
        }.attach()
    }

    private fun toEnterCompanyInfo() {
        startKtxActivity<EditCompanyInfoActivity>()
    }

    private fun toIdentityAuthentication() {
        startKtxActivity<IdentityAuthActivity>()
    }

}